﻿@{
    string from = "";
    from = Request.QueryString["from"];
    string sql = "";
    sql = Request.QueryString["sql"];

    string connString =  System.Configuration.ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
    MySql.Data.MySqlClient.MySqlConnection connection = new MySql.Data.MySqlClient.MySqlConnection(connString);
}

<!DOCTYPE html>

<html lang="pl">
    <head>
        <meta charset="utf-8" />
        <title>Layout</title>
        <link href="Images/favicon.ico" rel="shortcut icon" type="image/x-icon" />
        <link rel="stylesheet" type="text/css" href="Shared/styles.css">
    </head>

    <body>
        <!-- Header -->
        <p style="font-family: 'Segoe UI'; font-size: 45px; color: #fff; padding: 0px; margin: 0px 25px 0px 10px">Wyszukiwanie</p></a>
        <!-- Content -->
        <!-- Pola wyszukiwania -->
        <div style="width: 450px; height: auto; position: absolute; margin-left: 10px; margin-top: 10px">
            @switch(from)
            {
                case "hosts":
                {
                    <p style="font-family: 'Segoe UI'; font-size: 20px; color: #808080; margin: 0px 0px 20px 0px">Wyszukaj host</p>
                    <form action="Find.cshtml" style="color: #fff; font-family: 'Segoe UI'" method="post">
                        <a style="width: 205px; display: inline-block; margin-bottom: 7px">Nazwa:</a><input style="width: 230px" type="text" name="name"><br>
                        <a style="width: 205px; display: inline-block; margin-bottom: 7px">Adres fizyczny:</a><input style="width: 230px" type="text" name="hardware_ethernet"><br>
                        <a style="width: 205px; display: inline-block; margin-bottom: 7px">Adres logiczny:</a><input style="width: 230px" type="text" name="fixed_address"><br>
                        <a style="width: 201px; display: inline-block; margin-bottom: 7px">Podsieć:</a>
                        <select style="width: 234px" name="subnet">
                            <option></option>
                            @{
                                string select = "SELECT id,name FROM subnets;"; 

                                MySql.Data.MySqlClient.MySqlCommand cmd = new MySql.Data.MySqlClient.MySqlCommand(select, connection);
                    
                                connection.Open();
                                MySql.Data.MySqlClient.MySqlDataReader read = cmd.ExecuteReader();

                                while (read.Read())
                                {
                                    System.Data.IDataRecord rec = (System.Data.IDataRecord)read;
                                    string id = rec.GetString(0);
                                    string name = rec.GetString(1);
                            
                                    <option value="@id">@name</option>            
                                }

                                connection.Close();
                                read.Close();
                            }
                        </select><br>
                        <input type="hidden" value="hosts" name="from">
                        <input style="left: 400px; margin-top: 8px; margin-left: 360px" type="submit" value="Wyszukaj">
                    </form>
                    break;    
                }
                case "subnets":
                {
                    <p style="font-family: 'Segoe UI'; font-size: 20px; color: #808080; margin: 0px 0px 20px 0px">Wyszukaj podsieć</p>
                    <form action="Find.cshtml" style="color: #fff; font-family: 'Segoe UI'" method="post">
                        <a style="width: 205px; display: inline-block; margin-bottom: 7px">Nazwa:</a><input style="width: 230px" type="text" name="name"><br>
                        <a style="width: 205px; display: inline-block; margin-bottom: 7px">Adres:</a><input style="width: 230px" type="text" name="address"><br>
                        <a style="width: 205px; display: inline-block; margin-bottom: 7px">Maska podsieci:</a><input style="width: 230px" type="text" name="netmask"><br>
                        <a style="width: 205px; display: inline-block; margin-bottom: 7px">Adres rozgłoszeniowy:</a><input style="width: 230px" type="text" name="broadcast_address"><br>
                        <a style="width: 205px; display: inline-block; margin-bottom: 7px">Router:</a><input style="width: 230px" type="text" name="routers"><br>
                        <a style="width: 201px; display: inline-block; margin-bottom: 7px">Serwer:</a>
                        <select style="width: 234px" name="server">
                            <option></option>
                            @{
                                string select = "SELECT id,name FROM servers;"; 

                                MySql.Data.MySqlClient.MySqlCommand cmd = new MySql.Data.MySqlClient.MySqlCommand(select, connection);
                    
                                connection.Open();
                                MySql.Data.MySqlClient.MySqlDataReader read = cmd.ExecuteReader();

                                while (read.Read())
                                {
                                    System.Data.IDataRecord rec = (System.Data.IDataRecord)read;
                                    string id = rec.GetString(0);
                                    string name = rec.GetString(1);
                            
                                    <option value="@id">@name</option>            
                                }

                                connection.Close();
                                read.Close();
                            }
                        </select><br>
                        <a style="width: 205px; display: inline-block; margin-bottom: 7px">Budynek:</a><input style="width: 230px" type="text" name="building"><br>
                        <a style="width: 205px; display: inline-block; margin-bottom: 7px">Zakres:</a><input style="width: 230px" type="text" name="_range"><br>
                        <input type="hidden" value="subnets" name="from">
                        <input style="left: 400px; margin-top: 8px; margin-left: 360px" type="submit" value="Wyszukaj">
                    </form>
                    break;    
                }
                case "servers":
                {
                    <p style="font-family: 'Segoe UI'; font-size: 20px; color: #808080; margin: 0px 0px 20px 0px">Wyszukaj serwer</p>
                    <form action="Find.cshtml" style="color: #fff; font-family: 'Segoe UI'" method="post">
                        <a style="width: 205px; display: inline-block; margin-bottom: 7px">Nazwa:</a><input style="width: 230px" type="text" name="name"><br>
                        <a style="width: 205px; display: inline-block; margin-bottom: 7px">DNS:</a><input style="width: 230px" type="text" name="domain_name_servers"><br>
                        <a style="width: 205px; display: inline-block; margin-bottom: 7px">Domyślny czas dzierżawy:</a><input style="width: 230px" type="text" name="default_lease_time"><br>
                        <a style="width: 205px; display: inline-block; margin-bottom: 7px">Maksymalny czas dzierżawy:</a><input style="width: 230px" type="text" name="max_lease_time"><br>
                        <a style="width: 205px; display: inline-block; margin-bottom: 7px">Autorytatywny:</a><input style="width: 230px" type="text" name="authoritative"><br>
                        <a style="width: 205px; display: inline-block; margin-bottom: 7px">Log obiektu:</a><input style="width: 230px" type="text" name="log_facility"><br>
                        <input type="hidden" value="servers" name="from">
                        <input type="hidden" value="true" name="find">
                        <input style="left: 400px; margin-top: 8px; margin-left: 360px" type="submit" value="Wyszukaj">
                    </form>
                    break;    
                }
            }
        </div>
        <!-- Lista wyszukanych obiektów -->
        <div style="width: auto; height: auto; position: absolute; left: 460px; margin-top: 10px">
            @{
                switch(from)
                {
                    case "servers":
                    {
                        <p style="font-family: 'Segoe UI'; font-size: 20px; color: #808080; margin: 0px 0px 20px 0px">Lista wyszukanych serwerów</p>
                        break;
                    }
                    case "subnets":
                    {
                        <p style="font-family: 'Segoe UI'; font-size: 20px; color: #808080; margin: 0px 0px 20px 0px">Lista wyszukanych podsieci</p>
                        break;
                    }
                    case "hosts":
                    {
                        <p style="font-family: 'Segoe UI'; font-size: 20px; color: #808080; margin: 0px 0px 20px 0px">Lista wyszukanych stacji roboczych</p>
                        break;
                    }
                }
            }

            @{
                MySql.Data.MySqlClient.MySqlCommand command = null;
                MySql.Data.MySqlClient.MySqlDataReader reader = null;

                if(sql!=null)
                {
                    command = new MySql.Data.MySqlClient.MySqlCommand(sql, connection);
                    connection.Open();

                    reader = command.ExecuteReader();

                    <!-- Tiles -->
                    while (reader.Read())
                    {
                        System.Data.IDataRecord record = (System.Data.IDataRecord)reader;
                        string name = record.GetString(0);

                        string url = "Selected.cshtml?selected="+name+"&from=";

                        switch(from)
                        {
                            case "servers":
                            {
                                url += "servers";
                                break;
                            }
                            case "subnets":
                            {
                                url += "subnets";
                                break;
                            }
                            case "hosts":
                            {
                                url += "hosts";
                                break;
                            }
                        }

                        if(name.Length <= 12)
                        {
                            <a href="@url">
                                <div class="tile">
                                    <p>@record.GetString(0)</p>
                                </div>
                            </a>
                        }
                        else
                        {
                            string tmp = name.Substring(0,12)+"...";
                            <a href="@url">
                                <div class="tile">
                                    <p>@tmp</p>
                                </div>
                            </a>
                        }
                    }

                    connection.Close();
                    reader.Close();
                }
            }
        </div>

        <!-- Menu -->
        <div style="height: 30px; width: 100%; background-color: #0094ff; position: fixed; bottom: 0px; padding-left: 10px">

        </div>
    </body>
</html>